<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/17
 * Time: 19:43
 */

namespace Task\App\Water;

use Task\Core\Base;
use Task\Core\WechatApi;

class WaterData extends Base
{
    public $mysql;
    public $accountId;
    public $wxTplSign;//模板id


    public function __construct()
    {
        parent::__construct();
        $this->mysql = $this->selectDB('mysql');
        $this->accountId;

    }

    /*
     * 每天定时给有消息权限的管理员推送送
     *
     */

    function sendNews($accountId)
    {
        $this->accountId = $accountId;
        if ($accountId == 60) {
            $this->wxTplSign = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs';
        } elseif ($accountId == 5) {
            $this->wxTplSign = 'rTvzmVOqa6savYmetPXhu9cYtAXQOmoKnLPTgMzkJfY';
        }

        /*
         * 查找wechatid sql
         */
        $sql = "select * from gc_wechat where account_id=" . $this->accountId . " limit 1";
        $wechats = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);

        $wechat = new WechatApi($wechats[0], $this->accountId);

        /**
         * 查找订单超过24的未完成订单
         */
        $beforTime = strtotime(date('Y-m-d')) - 86399;
        $sql = "SELECT * FROM `gc_water_order` WHERE ( `status` < 3 ) AND ( `create_time` < $beforTime ) AND ( `status` !=0 ) AND ( `subarea_id` >= 0 ) AND ( `wechat_id` = {$wechats[0]['wechat_id']} )";
        $outOrder = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);

        /**
         * 根据园区楼幢获取管理员微信信息,没有设置的获取默认的。
         *
         * foreach ($outOrder as $key => $value) {
         * if ($value['community_name']) {
         * $mammger_sql = "SELECT * FROM `gc_water_manager` where address='{$value['community_name']}' and account_id= $accountId and cut_off= 0 limit 1";
         * $mananger = $this->mysql->query($mammger_sql)->fetchAll(\PDO::FETCH_ASSOC);
         *
         * if (!empty($mananger)) {
         * $manangerInfo[$key]['wechat_user_id'] = $mananger[0]['wechat_user_id'];
         * $manangerInfo[$key]['address'] = $value['community_name'] . $value['building_name'];
         *
         * } else {
         * $mammger_sql = "SELECT * FROM `gc_water_manager` where address='默认' and account_id= $accountId limit 1";
         * $mananger = $this->mysql->query($mammger_sql)->fetchAll(\PDO::FETCH_ASSOC);
         * $manangerInfo[$key]['wechat_user_id'] = $mananger[0]['wechat_user_id'];
         * $manangerInfo[$key]['address'] = $value['community_name'] . $value['building_name'];
         * }
         * }
         * }
         */
        /**
         * 给后台设置通知的人推送消息
         */
        $mammger_sql = "SELECT * FROM `gc_water_manager` where address='通知' and account_id= $accountId and cut_off= 0 limit 1";
        $mananger = $this->mysql->query($mammger_sql)->fetchAll(\PDO::FETCH_ASSOC);
        $info = array(
            'first' => array("value" => "您所管理的园区有未完成的订水订单"),
            'keyword1' => array("value" => "智能订水"),
            'keyword2' => array("value" => '超时订单数' .count($outOrder) . '单',),
            'remark' => array("value" => '请及时处理！'),
        );

        //在微信用户表里面获取对应的用户openids
        $wechat_user_id = $mananger[0]['wechat_user_id'];
        $openid_sql = "select openid from gc_wechat_user where wechat_id=" . $wechats[0]['wechat_id'] . " and  wechat_user_id=" . $wechat_user_id . " limit 1";
        $openids = $this->mysql->query($openid_sql)->fetchAll(\PDO::FETCH_ASSOC);
        $openid = $openids[0]['openid'];
        $url = "#";
        //实例化微信api
        $result = $wechat->messageTpl($wechats[0]['appid'], $wechats[0]['appsecret'], $openid, $this->wxTplSign, $info, $url);

    }

}